import GameConfig from "./GameConfig";


cc.Class({
    extends: cc.Component,

    properties: {
        level1: cc.Prefab,
        level2: cc.Prefab,
        level3: cc.Prefab,
    },

    start() {
        this.status = 0;
    },

    createInstance(lifeCount) {
        switch(lifeCount) {
            case 1: 
                return cc.instantiate(this.level1);
            case 2:
                return cc.instantiate(this.level2);
            case 3:
                return cc.instantiate(this.level3);
        }
        return null;
    },
    
    init(game, posY, lifeCount) {
        this.game = game;
        this.posY = posY;
        this.node.opacity = 255;
        this.lifeCount = lifeCount;
    },

    // 碰撞检测
    onCollisionEnter(other,self){
        this._backToPool();
        
        this.game.player.status = 1;
        this.game.player.defenseTimer = 0;
        this.game.player.defenseItem.getComponent('FollowDefense').updateLifeCount(this.lifeCount);
        this.game.player.defenseItem.opacity = 255;
    },

    _backToPool() {
        switch(this.lifeCount) {
            case 1: 
                this.game.defenseLevel1Pool.put(this.node);
                break;
            case 2: 
                this.game.defenseLevel2Pool.put(this.node);
                break;
            case 3: 
                this.game.defenseLevel3Pool.put(this.node);
                break;
        }
    },

    _move(posX, posY) {
        this.node.setPosition(cc.v2(posX, posY));
    },

    update (dt) {
        if (GameConfig.GAME_START) {
            this.posY -= GameConfig.PROPS_PLAYER_MOVE_SPEED;
            this._move(this.node.position.x, this.posY);
            // 超出显示范围，则放回池中
            if (this.posY <= -GameConfig.INIT_BARRIER_POSY) {
                this._backToPool();
            }
        }
    },
});
